﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.ApproveCommand;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.ApproveCommandHandler
{
    /// <summary>
    /// 添加请假申请表命令处理
    /// </summary>
    public class AddLeaveModelCommandHandler : IRequestHandler<AddLeaveModelCommand, APIResult<string>>
    {
        private readonly IBaseRepository<LeaveModel> leaveRepos;
        private readonly IMapper mapper;

        public AddLeaveModelCommandHandler(IBaseRepository<LeaveModel> leaveRepos,IMapper mapper)
        {
            this.leaveRepos = leaveRepos;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加请假申请表命令处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<string>> Handle(AddLeaveModelCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var model = mapper.Map<LeaveModel>(request);
            model.LeaveEndTime = model.LeaveEndTime.AddHours(8); 
            model.CreateTime = model.CreateTime.AddHours(8);
            int res = leaveRepos.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = "请假表"+ APIEnum.添加成功.ToString();
                result.data = res.ToString();
            }else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = "请假表" + APIEnum.添加失败.ToString();
                result.data = res.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
